<?php
/**
 * 功能：后台菜单控制器
 * 作者：魏安来
 * 日期：2017/01/09
 */

namespace app\index\controller;
use think\Db;
use think\Validate;
use app\index\model\Menu as menuModel;

class Menu extends Base{

	/*显示菜单列表*/
	public function index() {
		//调用左侧菜单
		$this->sidebar();
		$menuModel = new menuModel();
		$menuList = $menuModel->getAllMenu();
		$this->assign('menuList', $menuList);
		return $this->fetch();
	}

	/*查询左侧菜单*/
	public function sidebar() {
		$menuModel = new menuModel();
		$auth = new Auth();
		$group_id = $auth->getGroups(\think\Session::get('uid', 'tp'));
		//非超级管理员需要验证权限
		if($group_id[0]['group_id'] == 1) {
			$menuInfo = $menuModel->getAllMenu();
		} else {
			$menuInfo = $menuModel->hasAuthMenu(\think\Session::get('uid', 'tp'));
		}
		$this->assign('menuInfo', $menuInfo);
	}

	/*添加菜单*/
	public function add() {
		$menuModel = new menuModel();
		$menuArr = $menuModel->getAllMenu();
		$this->assign('menuArr', $menuArr);
		if (request()->isPost()) {
			//接收数据
			$data = input('post.');
			if (!isset($data['is_show'])) {
				$data['is_show'] = 0;
			} else {
				$data['is_show'] = 1;
			}
			//定义验证规则
            $rule = [
                /*unique:menu,name='.$data['name'].'&pid='.$data['pid'] 验证菜单名称是否存在menu表中*/
				'name' => 'require|min:2|unique:menu,name='.$data['name'].'&pid='.$data['pid'], 
				'model' => 'require',
				'controller' => 'require',
				'method' => 'require',
				'sort' => 'require'
            ];

            //定义错误提示信息
            $message = [
                'name.require' => '菜单名称不能为空',
				'name.min' => '菜单名称不能少于2个字符',
				'name.unique' => '菜单名称已存在',
				'model.require' => '模块名不能为空',
				'controller.require' => '控制器名称不能为空',
				'method.require' => '方法名不能为空',
				'sort.require' => '排序不能为空'
            ];
			$validate = new \think\Validate($rule, $message);
            $result = $validate->check($data);
			if ($result) {
				//写入数据库
				$affected = db('Menu')->insert($data);
				if ($affected) {
					return 'succ';
				} else {
					return 'error';
				}
			} else {
				return $validate->getError();
			}
		}
		return $this->fetch();
	}

	/*菜单排序*/
	public function sort() {
		if (request()->isAjax()) {
			$sortData=input('post.');
			$affectRow=0;
			foreach ($sortData as $k => $v) {
				$sortRes = Db::name('menu')->update(['id' => $k, 'sort' => $v]);
				$affectRow += $sortRes;
			}
			if ($affectRow) {
				return 'succ';
			} else {
				return 'error';
			}
		}
	}

	/*菜单编辑*/
	public function edit() {
		$menuModel = new menuModel();
		$menuArr = $menuModel->getAllMenu();
		//接收要编辑的菜单id
		$id = request()->param('id');
		//查找上级菜单
		$result=Db::name('menu')->where('id', $id)->find();
		$this->assign('pid', $result['pid']);
		$this->assign('menuArr', $menuArr);
		$this->assign('id', $id);
		/*获取需要编辑的菜单数据*/
		$oneMenuData = $menuModel->getOneMenuData($id);
		$this->assign('oneMenuData',$oneMenuData);
		if (request()->isAjax()) {
			$data = input('post.');
			$id = $data['id'];
			unset($data['id']);
			//处理是否显示字段
			if (!isset($data['is_show'])) {
				$data['is_show'] = 0;
			} else {
				$data['is_show'] = 1;
			}
			//如果没有修改菜单名称就不验证菜单是否已存在
			if (trim($data['name']) == trim($result['name'])) {
				//定义验证规则
	            $rule = [
	                //unique:menu,name='.$data['name'].'&pid='.$data['pid'] 验证菜单名称是否存在menu表中
					'name' => 'require|min:2', 
					'model' => 'require',
					'controller' => 'require',
					'method' => 'require',
					'sort' => 'require'
	            ];

	            //定义错误提示信息
	            $message = [
	                'name.require' => '菜单名称不能为空',
					'name.min' => '菜单名称不能少于2个字符',
					'name.unique' => '菜单名称已存在',
					'model.require' => '模块名不能为空',
					'controller.require' => '控制器名称不能为空',
					'method.require' => '方法名不能为空',
					'sort.require' => '排序不能为空'
	            ];
			} else {
				//定义验证规则
	            $rule = [
	                //unique:menu,name='.$data['name'].'&pid='.$data['pid'] 验证菜单名称是否存在menu表中 
					'name' => 'require|min:2|unique:menu,name='.$data['name'].'&pid='.$data['pid'], 
					'model' => 'require',
					'controller' => 'require',
					'method' => 'require',
					'sort' => 'require'
	            ];

	            //定义错误提示信息
	            $message = [
	                'name.require' => '菜单名称不能为空',
					'name.min' => '菜单名称不能少于2个字符',
					'name.unique' => '菜单名称已存在',
					'model.require' => '模块名不能为空',
					'controller.require' => '控制器名称不能为空',
					'method.require' => '方法名不能为空',
					'sort.require' => '排序不能为空'
	            ];
			}
			
			$validate = new \think\Validate($rule, $message);
            $result = $validate->check($data);
			if ($result){
				$affected = $menuModel->updataOneMenuData($id, $data);
				if($affected){
					return 'succ';
				}else{
					return 'error';
				}
			} else {
				return $validate->getError();
			}
		}
		return $this->fetch();
	}

	/*菜单删除*/
	public function delete() {
		$delete_id = request()->param('id');
		$menuModel = new menuModel();
		$affectRow = $menuModel->deleteMenu($delete_id);
		if ($affectRow) {
			return 'succ';
		} else {
			return 'error';
		}
	}

}


?>